package com.ayuku.commons;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class ReportViewerHTML extends org.eclipse.swt.widgets.Dialog {
    private Shell dialogShell;
    private Browser browserReport;
    private Button button2;
    private Button button3;
    private Button button1;

    public ReportViewerHTML(Shell parent, int style) {
        super(parent, style);
    }

    public void open(String html) {
        try {
            Shell parent = getParent();
            dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

            dialogShell.setLayout(new FormLayout());
            dialogShell.layout();
            dialogShell.pack();
            dialogShell.setSize(900, 600);
            {
            	button1 = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
            	FormData button1LData = new FormData();
            	button1LData.width = 54;
            	button1LData.height = 25;
            	button1LData.left =  new FormAttachment(0, 1000, 98);
            	button1LData.top =  new FormAttachment(0, 1000, 8);
            	button1.setLayoutData(button1LData);
            	button1.setText("Print");
            }

            {
                browserReport = new Browser(dialogShell, SWT.NONE);

                FormData browser1LData = new FormData();
                browser1LData.width = 879;
                browser1LData.height = 524;
                browser1LData.left =  new FormAttachment(0, 1000, 6);
                browser1LData.top =  new FormAttachment(0, 1000, 43);
                browser1LData.right =  new FormAttachment(1000, 1000, -7);
                browser1LData.bottom =  new FormAttachment(1000, 1000, -6);
                browserReport.setLayoutData(browser1LData);
                browserReport.setText("Loading .....");
                setbrowser(html);
            }
            {
            	button2 = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
            	FormData button2LData = new FormData();
            	button2LData.left =  new FormAttachment(0, 1000, 6);
            	button2LData.top =  new FormAttachment(0, 1000, 8);
            	button2LData.width = 31;
            	button2LData.height = 25;
            	button2.setLayoutData(button2LData);
            	button2.setText("<");
            }
            {
            	button3 = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
            	FormData button3LData = new FormData();
            	button3LData.left =  new FormAttachment(0, 1000, 48);
            	button3LData.top =  new FormAttachment(0, 1000, 8);
            	button3LData.width = 31;
            	button3LData.height = 25;
            	button3.setLayoutData(button3LData);
            	button3.setText(">");
            }

            dialogShell.setLocation(getParent().toDisplay(100, 100));
            dialogShell.open();

            Display display = dialogShell.getDisplay();

            while (!dialogShell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setbrowser(final String str) {
        Display.getDefault().asyncExec(new Runnable() {
                public void run() {
                    getBrowserReport().setText(str);
                }
            });
    }

    public final Browser getBrowserReport() {
        return browserReport;
    }

    public final void setBrowserReport(Browser browserReport) {
        this.browserReport = browserReport;
    }
}
